var order = require('../../data/order-data.js')

const app = getApp()
Page({
  data:{
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    gridCol: 3,
    beginDate:"2019-04-01",
    endDate: "2019-04-01",
    cubardate:"今日订单"     
  },
  

  onLoad: function getOrderData() {
    var that = this;
    var timestamp = Date.parse(new Date());

    var date = new Date(timestamp);
    console.log(timestamp);

    var Y = date.getFullYear().toString();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1).toString();
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate().toString();
    

    this.setData({
      beginDate: Y + "-" + M + "-" + D,
      endDate: Y + "-" + M + "-" + D
    })

    wx.request({
      url:
        "http://47.106.145.141:8080/api/v1/summary",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,

      },
      success: function (result) {
        console.log(result);
        that.setData({
          data: result.data.dataInfo,
        })

      }
    })


    wx.request({
      url: "http://47.106.145.141:8080/api/v1/record",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
        "pageNum": 1,
        "pageSize": 20,
        "type":1,
        "startDate": "2019-04-01",
        "endDate":"2019-04-30",
      },
      success: function (result) {
        // console.log(result.data.dataInfo.list[0].items[0].photo);
        console.log(result.data)
        console.log(result.data.dataInfo.list);        

        that.setData({
          todayOrder: result.data.dataInfo.list,
        })
        
        console.log(that.data.todayOrder);
    
      }   
    })
  },

  

  onPullDownRefresh: function getOrderData() {

    wx.stopPullDownRefresh();
    var that = this;

    var timestamp = Date.parse(new Date());

    var date = new Date(timestamp);
    console.log(timestamp);

    var Y = date.getFullYear().toString();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1).toString();
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate().toString();
  

    wx.request({
      url: "http://47.106.145.141:8080/api/v1/sellRecords",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
        "pageNum": 1,
        "pageSize": 6,
        "startDate": Y + "-" + M + "-" + D
      },
      success: function (result) {

        if (result.data.code == 4008) {
          wx.showToast({
            title: '暂时无相关销售记录',
          })
        }else {
          wx.showLoading({
            title: '正在加载',
            mask: true
          });

          console.log(result);
          that.setData({
            todayOrder: result.data.dataInfo.list
          })

          wx.hideLoading()

        }      

      }
    })

  },

  onOrderTap: function (e) {
    var orderData = JSON.stringify(e.currentTarget.dataset.orderdata);
    
  
    wx.navigateTo({
      url: 'order-detail/order-detail?orderdetail='+orderData,   
      //点击相应订单传递相应订单详情页
    })
  },


  beginDateChange: function (e) {
    var that = this;
    console.log('起始日期改变，携带值为', e.detail.value)
    this.setData({
      beginDate: e.detail.value
    })

    wx.showLoading({
      title: '正在加载',
      mask:true
    })
    wx.request({
      url: "http://47.106.145.141:8080/api/v1/sellRecords",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
        "pageNum": 1,
        "pageSize": 10,
        "startDate": that.data.beginDate,
        "endDate": that.data.endDate,
      },
      success: function (result) {
        
        console.log(result.data)
        
        if (result.data.code == 4008) {
          wx.hideLoading();
          wx.showToast({
            title: '暂时无相关销售记录',
          })

          var timestamp = Date.parse(new Date());

          var date = new Date(timestamp);
          console.log(timestamp);

          var Y = date.getFullYear().toString();
          var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1).toString();
          var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate().toString();


          that.setData({
            beginDate: Y + "-" + M + "-" + D,
            
          })

        }else {
          that.setData({
            todayOrder: result.data.dataInfo.list,
          })

          console.log(that.data.todayOrder);
          wx.hideLoading();
        }
      }
    })   
  },
  endDateChange: function (e) {
    var that = this;

    console.log('结束日期改变，携带值为', e.detail.value)

    this.setData({
      endDate: e.detail.value
    })

    wx.showLoading({
      title: '正在加载',
      mask: true
    })

    wx.request({
      url: "http://47.106.145.141:8080/api/v1/sellRecords",
      method: "GET",
      data: {
        "token": app.globalData.key,
        "shopId": app.globalData.shopId,
        "pageNum": 1,
        "pageSize": 10,
        "startDate": that.data.beginDate,
        "endDate": that.data.endDate,
      },
      success: function (result) {

        console.log(result.data)

        if (result.data.code == 4008) {
          wx.hideLoading();
          wx.showToast({
            title: '暂时无相关销售记录',
          })

          var timestamp = Date.parse(new Date());

          var date = new Date(timestamp);
          console.log(timestamp);

          var Y = date.getFullYear().toString();
          var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1).toString();
          var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate().toString();


          that.setData({
            beginDate: Y + "-" + M + "-" + D,

          })

        } else {
          that.setData({
            todayOrder: result.data.dataInfo.list,
          })

          console.log(that.data.todayOrder);
          wx.hideLoading();
        }
      }
    })
  },

  
  
  




 
})